
var chooseAiModelNo = [];
//关联AI模型弹框开启
function setChooseAiModel(){
    $.ajax({
        url : '/api/inter/eKnowChatSetController/getEKnowModelList',
        data : {},
        type : 'POST',
        async : false,
        success : function(result) {
            if(result.code=='SUCCESS'){
                chooseAiModelNo = chooseAiModelList;
                var eKnowModelList = result.eKnowModelList;
                var htmlStr = '<div class="layui-bg-gray" style="padding: 16px;max-height:calc(100vh - 90px);overflow: auto;"><div class="layui-row layui-col-space15">';
                if(eKnowModelList!=null && eKnowModelList.length>0){
                    for(var i=0;i<eKnowModelList.length;i++){
                        var isChooseed = false;
                        var id = eKnowModelList[i].id;
                        if(chooseAiModelNo!=null && chooseAiModelNo.length>0){
                            for(var j=0;j<chooseAiModelNo.length;j++){
                                if(chooseAiModelNo[j].id==id)isChooseed=true;
                            }
                        }
                        htmlStr+='<div class="layui-col-md6" style="cursor: pointer;" onclick="chooseModelByOpenPage(\''+id+'\',\''+eKnowModelList[i].modelOwnName+'\');">'+
                            '<div id="chooseView'+id+'" class="layui-card  choose-view-none '+(isChooseed?'choose-view':'')+'">'+
                            '<div class="layui-card-header"  style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;" title="'+eKnowModelList[i].modelOwnName+'">' +eKnowModelList[i].modelOwnName+
                            '<div style="float: right;" id="checkbox'+id+'" '+(isChooseed?'':'hidden')+'><i class="layui-icon layui-icon-ok" style="color: #4BB2FF;font-weight: bold;"></i></div>'+
                            '</div>'+
                            '<div class="layui-card-body" style="height: 100px;"><span class="list-span" title="'+eKnowModelList[i].modelDescribe+'">' +eKnowModelList[i].modelDescribe+'</span></div></div></div>';
                    }
                }
                htmlStr+='</div>'+
                    '<div class="bottom-view-open">'+
                    '<div class="layui-btn-container add-btn-view-open">'+
                    '<button type="button" class="layui-btn layui-bg-blue add-btn-open" onclick="yesChooseModelByOpenPage();">确定</button> '+
                    '</div>'+
                    '</div>';
                layer.open({
                    title:'配置AI大模型',
                    type: 1,
                    offset: 'r',
                    anim: 'slideLeft', // 从右往左
                    area: ['500px', '100%'],
                    shade: 0.5,
                    shadeClose: true,
                    closeBtn:0,
                    id: 'ID-demo-layer-aimodel',
                    content:htmlStr
                })
            }else{
                layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
            }
        }
    });
}

//弹框选择ai模型
function chooseModelByOpenPage(id,modelOwnName){
    var isChooseed = false;
    var newChooseList = [];
    if(chooseAiModelNo!=null && chooseAiModelNo.length>0){
        for(var j=0;j<chooseAiModelNo.length;j++){
            if(chooseAiModelNo[j].id==id){
                isChooseed=true;
            }else {
                newChooseList.push({id:chooseAiModelNo[j].id,modelOwnName:chooseAiModelNo[j].modelOwnName});
            }
        }
    }
    chooseAiModelNo = newChooseList;
    if(isChooseed){
        $('#chooseView'+id).removeClass('choose-view');
        $('#checkbox'+id).hide();
    }else {
        chooseAiModelNo.push({id:id,modelOwnName:modelOwnName});
        $('#chooseView'+id).addClass('choose-view');
        $('#checkbox'+id).show();
    }
}

//确认
function yesChooseModelByOpenPage() {
    layer.closeAll();
    chooseAiModelList = chooseAiModelNo;
    resetChooseModelSet();
    resetModelSelect();

}

//重新设置场景信息
function resetChooseModelSet(){
    var htmlStr = '';
    if(chooseAiModelList!=null && chooseAiModelList.length>0){
        for(var j=0;j<chooseAiModelList.length;j++){
            htmlStr+='<button type="button" class="layui-btn layui-btn-xs" style="margin-left:0px;margin-right: 10px;" onclick="delChooseModel(\''+chooseAiModelList[j].id+'\')">'+chooseAiModelList[j].modelOwnName+' <i class="layui-icon layui-icon-close layui-font-12"></i></button>';

        }
    }
    $('#chooseModelBtn').html(htmlStr);
}

//删除选择的AI模型
function delChooseModel(id){
    var newChooseList = [];
    if(chooseAiModelList!=null && chooseAiModelList.length>0){
        for(var j=0;j<chooseAiModelList.length;j++){
            if(chooseAiModelList[j].id!=id){
                newChooseList.push({id:chooseAiModelList[j].id,modelOwnName:chooseAiModelList[j].modelOwnName});
            }
        }
    }
    chooseAiModelList = newChooseList;
    resetChooseModelSet();
    resetModelSelect();
}

function resetModelSelect() {
    //选择ai模型下拉框
    if(chooseAiModelList!=null && chooseAiModelList.length>0){
        $('#chooseModel').html('');
        var htmlStr = '<select name="chooseAiModel" id="chooseAiModel" lay-filter="chooseAiModel">';
        for(var i=0;i<chooseAiModelList.length;i++){
            if(i==0){
                htmlStr+='<option value="'+chooseAiModelList[i].id+'" selected>'+chooseAiModelList[i].modelOwnName+'</option>';
                $('#aiModelId').val(chooseAiModelList[i].id);
            }else {
                htmlStr+='<option value="'+chooseAiModelList[i].id+'" >'+chooseAiModelList[i].modelOwnName+'</option>';
            }
        }
        htmlStr+= '</select>';
        $('#chooseModel').html(htmlStr);
    }else {
        var htmlStr = '<select name="chooseAiModel" id="chooseAiModel" lay-filter="chooseAiModel">';
        htmlStr+='<option value="" selected>请配置AI模型</option>';
        htmlStr+= '</select>';
        $('#chooseModel').html(htmlStr);
    }

    //选择所属分类
    layui.form.render('select');
}


